# -*- coding:utf-8 -*-

## =====================================================================
## Copyright (C) 2007-2011  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## =====================================================================


from Tkinter import *
import Pmw
import sys, os

class NotesGUI(Toplevel):
	def __init__(self, parent=None, field=None, title=u"Notes", path=None, log=None):
		Toplevel.__init__(self, parent)
		self.parent=parent
		self.grab_set()
		self.transient(self.master)
		self.resizable(width=False, height=False)
		self.field=field
		self.title(title)
		self.path=path
		self.log=log
		# Afficher un icon personnalisé
		try:
			if os.name =="posix":
				img = PhotoImage(file=self.path+"/icons/Notes-icon.gif")
				self.tk.call("wm", "iconphoto", self._w, img)
			else:
				self.iconbitmap(default=self.path+"/icons/Notes-icon.gif")
		except TclError:
			self.log.warning("Le window manager ne supporte pas l'icône que je veux lui mettre...")
		self.log.info("\tNotesGUI\tStart")
		self.makeNotes()

	def makeNotes(self):
		leftFrame = Frame(self)
		self.notesST = Pmw.ScrolledText(leftFrame,
							text_font='Helvetica 11 normal', text_bg ='ivory',
							text_padx =8, text_pady =8, text_wrap ='none',
							borderframe =1,
							borderframe_borderwidth =1,
							borderframe_relief =SOLID,
							usehullsize =1,
							hull_width =470, hull_height =340)
		self.notesST.pack(expand=YES, fill=BOTH, padx =4, pady =4)
		self.saveNotesBt=Button(leftFrame, text=u"Enregistrer", command=self.saveNotes)
		self.saveNotesBt.bind("<Return>", self.saveNotes)
		self.saveNotesBt.pack(fill=X)
		leftFrame.pack()
		# Inserer le contenu existant
		self.notesST.insert("0.0",self.field.get())

	def saveNotes(self, event=None):
		# TODO Supprimer le caractere \n a la fin du text
		self.field.set(self.notesST.get())
		self.destroy()